package cn.myjerry.thread.create;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Call {
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		// 创建线程
		ExecutorService ser = Executors.newFixedThreadPool(2);
		Race tortoise = new Race("小乌龟", 1000);
		Race rabbit = new Race("小兔子", 500);
		
		// 获取值
		Future<Integer> relust1 = ser.submit(tortoise);
		Future<Integer> relust2 = ser.submit(rabbit);
		
		Thread.sleep(2000);
		tortoise.setFlag(false);
		rabbit.setFlag(false);
		
		int num1 = relust1.get();
		int num2 = relust2.get();

		System.out.println("小乌龟跑了" + num1 + "步...");
		System.out.println("小兔子跑了" + num2 + "步...");
		
		// 停止服务
		ser.shutdownNow();
	}
}
class Race implements Callable<Integer>{
	private String Name;
	private long Time;
	private boolean Flag = true;
	private int Num;
	
	public Race() {
		super();
	}

	public Race(String name) {
		super();
		Name = name;
	}

	public String getName() {
		return Name;
	}

	public void setName(String name) {
		Name = name;
	}

	public long getTime() {
		return Time;
	}

	public void setTime(long time) {
		Time = time;
	}

	public boolean isFlag() {
		return Flag;
	}

	public void setFlag(boolean flag) {
		Flag = flag;
	}

	public int getNum() {
		return Num;
	}

	public void setNum(int num) {
		Num = num;
	}

	public Race(String name, long time) {
		super();
		Name = name;
		Time = time;
	}

	@Override
	public Integer call() throws Exception {
		while(Flag) {
			Thread.sleep(Time);
			Num++;
		}
		return Num;
	}
	
}